import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AlertController } from 'ionic-angular'; 

import { Http } from '@angular/http';
import { ActionSheetController } from 'ionic-angular';
declare let cordova;
declare let Wechat;


export interface WechatPayParam {
  partnerid: string;
  prepayid: string;
  noncestr: string;
  timestamp: string;
  sign: string;
}

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  obj:any;
  constructor(public navCtrl: NavController, public alertCtrl: AlertController,private http:Http,public actionSheetCtrl: ActionSheetController) {

  }
  public url: string = 'https://www.chinacademy.club/Alipay/pay/orderInfo.php'; //支付宝服务器端接口
 
  public wechat_url: string = 'https://www.chinacademy.club/Wechat/order.php'; //微信服务器端接口
  unescapeHTML(a){
    let aNew = "" + a;
       return aNew.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'");
   }
   //这个unescapeHTML方法就是我在https://blog.csdn.net/liujiawei00/article/details/78618499上看到的
   //因为我们后台返回的签名字符串里面的&符号默认是实体字符串的，但是安卓和ios的sdk会默认对其格式化成&符号，
   //而我们的js插件代码并没有做这个事情，所以我直接上传的签名字符串一直是实体字符串，所以传上去的不是&符号，一直调起不了支付宝。
   
   Alert(msg) {
      let alert = this.alertCtrl.create({
        title: 'Chinacademy',
        subTitle: msg,
        buttons: ['OK']
      });
      alert.present();
    }
   openPayActionSheet() {
    this.actionSheetCtrl.create({
      title: 'Payment Choice',
      buttons: [
        {
          text: "Alipay",
          handler: () => {
            this.Alipay(this.obj);
          }
        },
        {
          text: "Wechat",
          handler: () => {
            this.Wechatpay(this.obj);
          }
        },
        {
          text: "微信分享",
          handler: () => {
            this.WechatShare(0);
          }
        },
        {
          text: "微信朋友圈分享",
          handler: () => {
            this.WechatShare(1);
          }
        },
        {
          text:"Cancel",
          role: 'cancel'
        }
      ]
    }).present();

  }

  Alipay(obj){
    this.http.post(this.url,
      {
        "price":0.01,
        "description":"APP支付测试",
      })
      .subscribe(
          (val) => {
              let payInfo=this.unescapeHTML(val.text());
              console.log(payInfo);
                    
              cordova.plugins.MyAlipay.coolMethod(payInfo,
              (msg) => {
                  this.Alert("Payment Successed！");
              },
              (msg) => {
                  this.Alert("Payment Failed！");
              }
      );
          },
          response => {
              console.log("POST call in error", response);
          },
          () => {
              console.log("The POST observable is now completed.");
      });


  }
  
  Wechatpay(obj) {
    this.http.post(this.wechat_url,
      {
          "price":0.01   //传参，可以自由添加，在服务端设置接收即可
      })
      .subscribe(
          (val) =>{
           let  data = JSON.parse(val.text());
           console.log(data)
           var params = {
            partnerid:"1512108901", // merchant id
            prepayid: data.prepayid, // prepay id
            noncestr: data.noncestr, // nonce
            sign: data.sign, // signed string
            timestamp: data.timestamp+"",  // +“” 转化为字符串，服务端获取过来为数字类型
          }
          console.log(params)
          let me = this; //申明中间变量
          Wechat.sendPaymentRequest(params, function () {
             me.Alert("Success");
          }, function (reason) {
             me.Alert("Failed: " + reason);
          });

      });
  }
  WechatShare(sence){
    let me = this;
    Wechat.isInstalled(function (installed) {
      if(!installed){
        return false;
      }   
    },function(reason){
      me.Alert(reason);
    });
    Wechat.share({
      message: {
          title: "来搞来搞",
          description: "小栋栋的微信测试",
          thumb: "www/img/1.jpg",
          media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl: "http://shouji.baidu.com/software/24484079.html"
          }
      },
      scene: sence 
  }, function () {
      me.Alert("Share Successed！")
  }, function (reason) {
      me.Alert(reason);
  });
  
  }
}

